/**
 * 
 */
package com.desksoft.ecommerce.util.result;

import static com.google.common.base.Preconditions.checkNotNull;

import java.io.Serializable;
import java.text.MessageFormat;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.desksoft.ecommerce.entity.mall.dto.MallMessage;
import com.desksoft.ecommerce.helper.MessageHelper;
import com.desksoft.ecommerce.helper.mall.MallMsgHelper;
import com.desksoft.utility.string.StringUtil;
import com.google.common.collect.Lists;

/***************************************
 * 版权所有：杭州迪科软件 保留所有权利 创建日期: 创建作者：yejw 文件名称： 版本： 功能： 最后修改时间： 修改记录：
 *****************************************/
public class ServiceResult<E> implements Serializable {
	/**
	 * Logger for this class
	 */
	private static final Log logger = LogFactory.getLog(ServiceResult.class);

	private static final long serialVersionUID = -3683814744456689043L;

	protected final static String SUCCESS_CODE = "0";

	protected final static String ERROR_CODE = "x";

	/**
	 * @deprecated messageID
	 */
	protected String msgId;

	/**
	 * 结果代码 0：成功 非0：错误代码
	 */
	private String code;
	/**
	 * 成功code
	 */
	private String successCode;
	

	/**
	 * 结果消息
	 */
	private String msg;

	/**
	 * 保存结果信息
	 */
	private E result;

	/**
	 * 消息参数
	 */
	private List<String> msgParamList;

	/**
	 * 是否已调用判断成功方法
	 */
	private transient boolean hasCallIsSuccess = false;

	public ServiceResult() {
	}

	public ServiceResult(String code, String msg, E result) {
		this.code = code;
		this.msg = msg;
		this.result = result;
	}

	/**
	 * 判断是否成功
	 * 
	 * @return
	 */
	public boolean isSuccess() {
		if (logger.isDebugEnabled()) {
			this.hasCallIsSuccess = true;
		}
		return this.code == null || SUCCESS_CODE.equals(this.code);
	}
	
	/**
	 * 根据站点ID和successCode获取提示信息实例
	 * 
	 * @param siteID
	 * @return
	 */
	public MallMessage getSuccessMsg(Integer siteID) {
		
		MallMessage mm = null;
		if (this.isSuccess()) {

			mm = (this.msgParamList != null && !this.msgParamList.isEmpty()) ? MallMsgHelper
					.get(siteID, this.successCode, this.msgParamList.toArray())
					: MallMsgHelper.get(siteID, this.successCode);

			checkNotNull(mm, StringUtil.join("successCode: ", this.successCode, " 对应成功代码没有定义!"));

			return mm;
			
		}else{

			mm = new MallMessage();
			mm.setContent(this.msg);
			mm.setId(-1L);
			mm.setMsgid(this.successCode);
			mm.setMsgtype("ERROR");
			mm.setShowtype("POPUP");
			mm.setSiteid(siteID);

			return mm;
	
		}

		
	}
	/**
	 * 根据站点ID和CODE获取提示信息实例
	 * 
	 * @param siteID
	 * @return
	 */
	public MallMessage getMallMsg(Integer siteID) {
		if (this.isSuccess()) {
			return null;
		}

		MallMessage mm = null;

		if (ERROR_CODE.equals(this.code)) {
			mm = new MallMessage();
			mm.setContent(this.msg);
			mm.setId(-1L);
			mm.setMsgid(this.code);
			mm.setMsgtype("ERROR");
			mm.setShowtype("POPUP");
			mm.setSiteid(siteID);

			return mm;
		}

		mm = (this.msgParamList != null && !this.msgParamList.isEmpty()) ? MallMsgHelper
				.get(siteID, this.code, this.msgParamList.toArray())
				: MallMsgHelper.get(siteID, this.code);

		checkNotNull(mm, StringUtil.join("code: ", this.code, " 对应错误代码没有定义!"));

		return mm;
	}

	/**
	 * 根据CODE获取提示信息
	 * 
	 * @return
	 */
	public String getResourceMsg() {
		if (this.isSuccess()) {
			return null;
		}

		if (ERROR_CODE.equals(this.code)) {
			return this.msg;
		}

		String resourceMsg = MessageHelper.get(this.code);
		checkNotNull(resourceMsg,
				StringUtil.join("code: ", this.code, " 对应错误代码没有定义!"));

		// 替换参数
		if (this.msgParamList != null && !this.msgParamList.isEmpty()) {
			resourceMsg = MessageFormat.format(resourceMsg,
					this.msgParamList.toArray());
		}

		return resourceMsg;
	}

	/**
	 * 返回错误消息
	 * 
	 * @return
	 */
	public String getErrorMsg() {
		return StringUtil.join("[", this.code, "] ", this.getResourceMsg());
	}

	/**
	 * 添加错误参数
	 * 
	 * @param p
	 */
	public void addMsgParam(String p) {
		if (this.msgParamList == null) {
			this.msgParamList = Lists.newLinkedList();
		}
		this.msgParamList.add(p);
	}
	
	/**
	 * 添加错误参数
	 * 
	 */
	public void addMsgParam(List<String> p){
		if (this.msgParamList == null) {
			this.msgParamList = Lists.newLinkedList();
		}
		this.msgParamList.addAll(p);
	}

	/**
	 * 获取错误参数
	 * 
	 * @return
	 */
	public List<String> getMsgParam() {
		return this.msgParamList;
	}

	/**
	 * 设置成错误代码
	 */
	public void setErrorCode() {
		this.setCode(ERROR_CODE);
	}

	public String getSuccessCode() {
		return successCode;
	}

	public void setSuccessCode(String successCode) {
		this.successCode = successCode;
	}
	/**
	 * @return the code
	 */
	public String getCode() {
		return code;
	}

	/**
	 * @param code
	 *            the code to set
	 */
	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * @param msg
	 *            the msg to set
	 */
	public void setMsg(String msg) {
		this.msg = msg;
	}

	/**
	 * @return the result
	 */
	public E getResult() {
		if (logger.isDebugEnabled()) {
			if (!this.hasCallIsSuccess) {
				throw new RuntimeException(
						"请先调用isSuccess方法判断服务调用是否成功再调用获取结果方法!");
			}
		}
		return result;
	}

	/**
	 * @param result
	 *            the result to set
	 */
	public void setResult(E result) {
		this.result = result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return this.isSuccess() ? StringUtil.join("success: ", this.result)
				: StringUtil.join("failure: ", this.getErrorMsg());
	}

	/**
	 * @deprecated
	 * @return
	 */
	public String getMsgId() {
		return msgId;
	}

	/**
	 * @deprecated
	 * @param msgId
	 */
	public void setMsgId(String msgId) {
		this.msgId = msgId;
	}
}
